﻿Imports System.Data.OleDb
Imports System.Data
Partial Class ArticleDetails
    Inherits System.Web.UI.Page

    Protected Sub btnLike_Click(sender As Object, e As System.EventArgs) Handles btnLike.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
        oleDbConn.Open()
        '  Dim LikeSql As String = "Insert into tblLikes(ArticleID,Reader) Values (@f1,@f2)"
        ' Dim addLikesCmd As OleDbCommand = New OleDbCommand(LikeSql, oleDbConn)
        'addLikesCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        'addLikesCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        'addLikesCmd.ExecuteNonQuery()
        'btnLike.Visible = False
        'lblMessage.Visible = True
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        'Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
        'oleDbConn.Open()
        'Dim ShowLikeSql As String = "Select * from tblLikes where ArticleID = @f1"
        'Dim ShowLikecmd As OleDbCommand = New OleDbCommand(ShowLikeSql, oleDbConn)
        'ShowLikecmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        ' ShowLikescmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        'Dim ole

        If IsPostBack = False Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
            oleDbConn.Open()
            Dim ArtViewSql As String = "INSERT INTO tblViewArticles(Neighbor, Article_fkid) VALUES(@f1,@f2)"
            Dim cmd As OleDbCommand = New OleDbCommand(ArtViewSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
            cmd.ExecuteNonQuery()
        End If
    End Sub

    Protected Sub DVArticles_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.DetailsViewPageEventArgs) Handles DVArticles.PageIndexChanging
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
        Dim SqlString As String = "Insert into tblArtTagJun(ArticleID,ArtTag) Values(@f1,@f2)"
        oleDbConn.Open()


        For Each cb As ListItem In cbListTag.Items
            If cb.Selected Then
                'here is where the join to the table of tags and articles
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        Dim newTags = tbTag.Text.Split(",")
        For Each newTag In newTags
            'added new tag here...
            Dim newTagSql = "Insert INTO tblTagArticle(Tag) VALUES(@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            'finding if of new tag
            Dim getLastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID from TblTagArticle ORDER BY ID DESC", oleDbConn)
            Dim dr = getLastIdCmd.ExecuteReader()
            dr.Read()
            Dim lastID = dr.GetValue(0)

            'associate this last id with the article id
            Dim artcmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            artcmd.CommandType = CommandType.Text
            artcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            artcmd.Parameters.AddWithValue("@f2", lastID)
            artcmd.ExecuteNonQuery()

        Next

    End Sub

    Protected Sub btnComment_Click(sender As Object, e As System.EventArgs) Handles btnComment.Click
        System.Threading.Thread.Sleep(2000)
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
        Dim SqlString As String = "Insert into tblArtComments(Comments,title,SubmittedDate,Article_fkid,Author) Values (@f1,@f2,@f3,@f4,@f5)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim SubmittedDate As DateTime = Now()
        Dim format As String = "ddd MMM yyyy"
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", txtComment.Text)
        cmd.Parameters.AddWithValue("@f2", txtTitle.Text)
        cmd.Parameters.AddWithValue("@f3", SubmittedDate.ToString(format))
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f5", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
    End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
        Dim SqlString As String = "Insert into tblArtTagJun(ArticleID,ArtTag) Values(@f1,@f2)"
        oleDbConn.Open()

        For Each cb As ListItem In cbListTag.Items
            If cb.Selected Then
                'here is where the join to the table of tags and articles
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next
    End Sub
End Class
